using SmartCommunity.WebApi.Services.System;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.WebApi.Controllers.System
{
    /// <summary>
    /// API接口：角色
    /// </summary>
    [ApiController]
    [Route("api/system/[controller]")]
    public class RoleController : BaseController
    {
        #region 构造函数
        private readonly RoleService roleService;
        public RoleController(RoleService roleService)
        {
            this.roleService = roleService;
        }
        #endregion

        #region 查询导出 
        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpGet]
        public async Task<PageResult<RoleDto>> GetList([FromQuery] RoleQuery query, [FromQuery] Pager pager)
        {
            return await roleService.FindPagedList(query, pager, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<RoleDto> GetOne(int id)
        {
            return await roleService.FindRoleById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] RoleQuery query)
        {
            var pageData = await roleService.FindPagedList(query, Pager.Unpaged, CurrentUser);
            var exportor = new ExcelExporter();
            exportor.AddTitleRow(0, "角色编码", "角色名称", "备注说明", "角色状态", "创建人", "创建时间");
            int rowIndex = 1;
            foreach (var dto in pageData.DataList)
            {
                exportor.AddDataRow(rowIndex++, 0, dto.RoleCode, dto.RoleName, dto.Remark, dto.Disabled ? "作废" : "正常", dto.CreatedBy, dto.CreatedTime);
            }
            var bytes = exportor.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"角色导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增角色
        /// </summary>
        [HttpPost]
        [Log("新增角色", OperateType.Create, false, true)]
        public async Task<ActionResult<RoleDto>> Create([FromBody] RoleDto dto)
        {
            var role = await roleService.CreateRole(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = role.RoleId }, role);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改角色", OperateType.Update)]
        public async Task<ActionResult> Update(int id, [FromBody] RoleDto dto)
        {
            if (id != dto.RoleId)
            {
                return ErrorMessage("要更新的数据和ID不匹配");
            }
            await roleService.UpdateRole(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        [HttpDelete]
        [Log("删除角色", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] int[] ids)
        {
            if (ids.Length == 0)
            {
                return ErrorMessage("参数错误：没有接收到要删除的主键值");
            }
            await roleService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
